package com.ubi.core.framework.exception;

import java.util.List;
import java.util.Map;

public class BatchOperationExceptionBuilder {

    /**
     * @param totalCount       操作记录的总数
     * @param operation        操作的名称，比如：delete,mask,publish
     * @param exceptionContext 操作出现异常的上下文,其中key是异常的原因比如：not.exists,not.allow 定义的message key的规则是
     *                         default.{operation}.summary.message, default.{operation}.summary.{}.message
     * @return
     */
    public static BatchOperationException buildBatchOperationException(int totalCount, String operation,
        Map<String, List<Long>> exceptionContext) {
        BatchOperationException batchOperationException = new BatchOperationException();
        batchOperationException.setTotalCount(totalCount);
        batchOperationException.setSummaryMessageKey(String.format("default.%s.summary.message", operation));
        for (Map.Entry<String, List<Long>> entry : exceptionContext.entrySet()) {
            batchOperationException.getBatchOperationExceptionItems()
                .add(buildItem(entry.getValue(), operation, entry.getKey()));
        }
        return batchOperationException;
    }

    @Deprecated
    public static BatchOperationException buildBatchDeleteException(BatchDeleteException batchDeleteException) {
        BatchOperationException batchOperationException = new BatchOperationException();
        batchOperationException.setTotalCount(batchDeleteException.getTotalDeleteCount());
        batchOperationException.setSummaryMessageKey("default.delete.summary.message");
        batchOperationException.getBatchOperationExceptionItems()
            .add(buildNotAllowedItem(batchDeleteException.getReferByOthersIdList(), "delete"));
        batchOperationException.getBatchOperationExceptionItems()
            .add(buildNotExistsItem(batchDeleteException.getNotExistsIdList(), "delete"));
        return batchOperationException;
    }

    @Deprecated
    public static BatchOperationException buildBatchPublishException(int totalCount,
        List<Long> notAllowedOperationIdList, List<Long> notExistsIdList) {
        BatchOperationException batchOperationException = new BatchOperationException();
        batchOperationException.setTotalCount(totalCount);
        batchOperationException.setSummaryMessageKey("default.publish.summary.message");
        batchOperationException.getBatchOperationExceptionItems()
            .add(buildNotAllowedItem(notAllowedOperationIdList, "publish"));
        batchOperationException.getBatchOperationExceptionItems().add(buildNotExistsItem(notExistsIdList, "publish"));
        return batchOperationException;
    }

    @Deprecated
    public static BatchOperationException buildBatchRevokeException(int totalCount,
        List<Long> notAllowedOperationIdList, List<Long> notExistsIdList) {
        BatchOperationException batchOperationException = new BatchOperationException();
        batchOperationException.setTotalCount(totalCount);
        batchOperationException.setSummaryMessageKey("default.revoke.summary.message");
        batchOperationException.getBatchOperationExceptionItems()
            .add(buildNotAllowedItem(notAllowedOperationIdList, "revoke"));
        batchOperationException.getBatchOperationExceptionItems().add(buildNotExistsItem(notExistsIdList, "revoke"));
        return batchOperationException;
    }

    @Deprecated
    public static BatchOperationException buildBatchMaskException(int totalCount, List<Long> notAllowedOperationIdList,
        List<Long> notExistsIdList) {
        BatchOperationException batchOperationException = new BatchOperationException();
        batchOperationException.setTotalCount(totalCount);
        batchOperationException.setSummaryMessageKey("default.mask.summary.message");
        batchOperationException.getBatchOperationExceptionItems()
            .add(buildNotAllowedItem(notAllowedOperationIdList, "mask"));
        batchOperationException.getBatchOperationExceptionItems().add(buildNotExistsItem(notExistsIdList, "mask"));
        return batchOperationException;
    }

    static BatchOperationExceptionItem buildItem(List<Long> idList, String operation, String failedReason) {
        BatchOperationExceptionItem batchOperationExceptionItem = new BatchOperationExceptionItem();
        batchOperationExceptionItem.setIdList(idList);
        batchOperationExceptionItem
            .setSummaryMessageKey(String.format("default.%s.summary.%s.message", operation, failedReason));
        batchOperationExceptionItem
            .setItemSummaryMessageKey(String.format("default.%s.item.%s.message", operation, failedReason));
        return batchOperationExceptionItem;
    }

    static BatchOperationExceptionItem buildNotExistsItem(List<Long> idList, String operation) {
        return buildItem(idList, operation, "not.exists");
    }

    static BatchOperationExceptionItem buildNotAllowedItem(List<Long> idList, String operation) {
        return buildItem(idList, operation, "not.allowed");
    }
}
